package beans;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author CDI215
 */
public class Panier implements Serializable {
    private HashMap<String, Article> panier;
    
    public Panier() {
        panier = new HashMap();
    }

    public void add(String ref){
        if (panier.containsKey(ref)) {
            modify(ref, +1);
        } else {
            Article a = new Article();
            try {
                a.setLivre(Integer.valueOf(ref));
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Panier.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(Panier.class.getName()).log(Level.SEVERE, null, ex);
            }
            panier.put(ref, a);
        }
    }

    public void inc(String ref) {
        modify(ref, +1);
    }

    public void dec(String ref) {
        modify(ref, -1);
    }

    private void modify(String ref, int qty) {
        if (panier.containsKey(ref)) {
            Article a = panier.get(ref);
            a.setQty(a.getQty() + qty);
            if (a.getQty() < 1) {
                del(ref);
            }
        }
    }

    public void del(String ref) {
        if (panier.containsKey(ref)) {
            panier.remove(ref);
        }
    }

    public void clear() {
        panier.clear();
    }

    public int nbProduit() {
        return panier.size();
    }

    public float total() {
        return -1F;
    }

    public Collection<Article> list() {
        return panier.values();
    }

    public boolean isEmpty() {
        return panier.isEmpty();
    }

    public void load() {
    }

    public void save() {
    }

    public void commander() {
    }

    
    
    
    
}
